﻿Public Class PlanAmGest

    Dim db As New Connexion
    Dim Entrer, EntrerRes, Action, ActiveEsp, Selection As String
    Dim idPAG As Integer
    Public databaseName As String

    Private Sub PlanAmGest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Width = GroupBox2.Width + ButtonReslt.Width + 35
        Me.GroupBox2.Visible = True
        Me.GroupBox3.Visible = False
        Me.GroupBox1.Visible = False
        Me.ButtonReslt.Visible = True
        Me.ButtonAction.Visible = False

        MajPAG()
        MajCatgPAG()

    End Sub

    Private Sub ButtonReslt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReslt.Click
        Try
            If Me.GroupBox2.Visible = True Then
                Me.GroupBox2.Visible = False
                Me.GroupBox3.Visible = False
                Me.GroupBox1.Visible = True
                Me.ButtonAction.Visible = True
                Me.Width = GroupBox1.Width + ButtonReslt.Width + Me.ButtonAction.Width + 37
                Me.ButtonReslt.Left = Me.GroupBox2.Location.X
                Me.GroupBox1.Left = Me.GroupBox2.Location.X + ButtonReslt.Width + 5
                Me.ButtonAction.Left = Me.GroupBox1.Width + Me.ButtonReslt.Width + 23
                Me.GroupBox1.Top = Me.GroupBox2.Location.Y
                Dim img1 As Image
                img1 = Image.FromFile(WorkSpaceProjectName & "left-1.png")
                Me.ButtonReslt.Image = img1
            Else
                Me.Width = GroupBox2.Width + Me.ButtonReslt.Width + 30
                Me.GroupBox2.Visible = True
                Me.GroupBox1.Visible = False
                Me.GroupBox3.Visible = False
                Me.ButtonAction.Visible = False
                Me.ButtonReslt.Left = Me.GroupBox2.Width + 10
                Dim img1 As Image
                img1 = Image.FromFile(WorkSpaceProjectName & "right-1.png")
                Me.ButtonReslt.Image = img1
            End If
            Entrer = ""
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ButtonAction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAction.Click
        Try
            If Me.GroupBox1.Visible = True Then
                Me.GroupBox2.Visible = False
                Me.GroupBox3.Visible = True
                Me.GroupBox1.Visible = False
                Me.ButtonReslt.Visible = False
                Me.ButtonAction.Visible = True
                Me.Width = GroupBox3.Width + ButtonAction.Width + 35
                Me.ButtonAction.Left = Me.GroupBox2.Location.X
                Me.GroupBox3.Left = Me.GroupBox2.Location.X + ButtonAction.Width + 5
                Me.GroupBox3.Top = Me.GroupBox2.Location.Y

                Dim img1 As Image
                img1 = Image.FromFile(WorkSpaceProjectName & "left-1.png")
                Me.ButtonAction.Image = img1
            Else
                Me.Width = GroupBox1.Width + Me.ButtonAction.Width + Me.ButtonReslt.Width + 30
                Me.GroupBox2.Visible = False
                Me.GroupBox1.Visible = True
                Me.GroupBox3.Visible = False
                Me.ButtonReslt.Visible = True
                Me.ButtonAction.Visible = True
                Me.Width = GroupBox1.Width + ButtonReslt.Width + Me.ButtonAction.Width + 37
                Me.ButtonReslt.Left = Me.GroupBox2.Location.X
                Me.GroupBox1.Left = Me.GroupBox2.Location.X + ButtonReslt.Width + 5
                Me.ButtonAction.Left = Me.GroupBox1.Width + Me.ButtonReslt.Width + 23
                Dim img1 As Image
                img1 = Image.FromFile(WorkSpaceProjectName & "right-1.png")
                Me.ButtonAction.Image = img1
            End If
            Entrer = ""
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MajPAG()
        db.myReq = "Select * from [dbo].[PLANAMGEST]"
        db.Connecter(databaseName)
        DataGridViewPAG.Rows.Clear()
        While db.rdr.Read()
            DataGridViewPAG.Rows.Add(db.rdr(0), db.rdr(1), db.rdr(2), db.rdr(3), db.rdr(4), db.rdr(5), db.rdr(6), db.rdr(7), db.rdr(8), db.rdr(9), db.rdr(10), db.rdr(11), db.rdr(12), db.rdr(13))
        End While
        db.Disconnecter()
    End Sub

    Private Sub ButtonNouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouv.Click
        Me.TextBoxIntit.Text = ""
        Me.TextBoxCircElab.Text = ""
        Me.TextBoxMaitOeuv.Text = ""
        Me.TextBoxMaitOuvr.Text = ""
        Me.TextBoxAut.Text = ""
        Me.TextBoxEspGeo.Text = ""
        Me.TextBoxSrcFian.Text = ""
        Me.TextBoxMenOeuv.Text = ""
        Action = "Ajouter"
    End Sub

    Private Sub MajCatgPAG()
        db.myReq = "Select * from [dbo].[TypPAG] "
        db.Connecter(databaseName)
        Me.ComboBoxIdCatgPAG.Items.Clear()
        Me.ComboBoxCatg.Items.Clear()

        ComboBoxIdCatgPAG.Items.Add("")
        ComboBoxCatg.Items.Add("")

        Do While db.rdr.Read()
            Me.ComboBoxCatg.Items.Add(db.rdr(2))
            Me.ComboBoxIdCatgPAG.Items.Add(db.rdr(0))
        Loop
        ComboBoxIdCatgPAG.SelectedIndex = 0
        ComboBoxCatg.SelectedIndex = 0
        db.Disconnecter()

    End Sub

    Private Sub ButtonEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnregistrer.Click
        If Action = "Ajouter" Then
            Dim dateRecConv As Date = Me.DateTimePickerDate.Value.Date
            Dim sDate As String = dateRecConv.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture)

            db.myReq = " INSERT INTO [dbo].[PLANAMGEST]([Id_Ref],[Id_TypPAG],[ID_Sit],[NOM_PlnGst],[CADRE_PLNGST],[FINCMT_PlnGst],[DATADAT_PlnGst],[MOVRGE_PlnGst],[MOVRE_PlnGst],[AUT_PlnGst],[ESPGEO_PlnGst],[OBJ_PlnGst],[ORG_PlnGst]) VALUES('0','" & Me.ComboBoxIdCatgPAG.Text & "','" & db.idSite & "','" & Me.TextBoxIntit.Text & "','" & Me.TextBoxCircElab.Text & "','" & Me.TextBoxSrcFian.Text & "','" & sDate & "','" & Me.TextBoxMaitOuvr.Text & "','" & Me.TextBoxMaitOeuv.Text & "','" & Me.TextBoxAut.Text & "','" & Me.TextBoxEspGeo.Text & "','" & Me.TextBoxObj.Text & "','" & Me.TextBoxMenOeuv.Text & "')"
            db.Connecter(databaseName)
            db.Disconnecter()
            MajPAG()
        End If
    End Sub

    Private Sub ComboBoxCatg_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxCatg.Click
        Selection = "active"
    End Sub
    Private Sub ComboBoxCatg_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxCatg.TextChanged
        Try
            If Selection = "active" Then
                If Me.ComboBoxCatg.FindString(Me.ComboBoxCatg.Text) > 0 Then
                    Dim Position As Int32 = Me.ComboBoxCatg.Text.Length
                    Me.ComboBoxCatg.SelectedIndex = Me.ComboBoxCatg.FindString(Me.ComboBoxCatg.Text)
                    Me.ComboBoxCatg.SelectionStart = Position
                    Me.ComboBoxCatg.SelectionLength = Me.ComboBoxCatg.Text.Length - Position
                    Me.ComboBoxIdCatgPAG.SelectedIndex = Me.ComboBoxCatg.FindString(Me.ComboBoxCatg.Text)
                Else
                    ComboBoxCatg.SelectedIndex = 0
                    ComboBoxIdCatgPAG.SelectedIndex = 0
                End If
            End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub DataGridViewPAG_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridViewPAG.CurrentCellChanged
        If EntrerRes = "debut" Then
            selectionResRec()
        End If
    End Sub
    Private Sub DataGridViewPAG_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridViewPAG.MouseClick
        If e.Button = MouseButtons.Left And DataGridViewPAG.CurrentRow.Index < DataGridViewPAG.RowCount - 1 Then
            EntrerRes = "debut"
            selectionResRec()
        End If
    End Sub
    Private Sub selectionResRec()
        If EntrerRes = "debut" Then
            ActiveEsp = ""
            Me.TextBoxIntit.Text = Me.DataGridViewPAG.SelectedCells(4).Value
            Me.TextBoxCircElab.Text = Me.DataGridViewPAG.SelectedCells(5).Value
            Me.TextBoxMaitOeuv.Text = Me.DataGridViewPAG.SelectedCells(8).Value
            Me.TextBoxMaitOuvr.Text = Me.DataGridViewPAG.SelectedCells(9).Value
            Me.TextBoxAut.Text = Me.DataGridViewPAG.SelectedCells(10).Value
            Me.TextBoxEspGeo.Text = Me.DataGridViewPAG.SelectedCells(11).Value
            Me.DateTimePickerDate.Text = Me.DataGridViewPAG.SelectedCells(7).Value
            Me.TextBoxSrcFian.Text = Me.DataGridViewPAG.SelectedCells(6).Value
            Me.TextBoxMenOeuv.Text = Me.DataGridViewPAG.SelectedCells(13).Value
            Me.TextBoxObj.Text = Me.DataGridViewPAG.SelectedCells(12).Value
            Me.ComboBoxCatg.SelectedIndex = Me.ComboBoxIdCatgPAG.Items.IndexOf(Me.DataGridViewPAG.SelectedCells(2).Value)
            idPAG = Me.DataGridViewPAG.SelectedCells(0).Value
        End If
    End Sub
End Class